/**
 * @file pm2的配置文件
 * @author zhangteng11@baidu.com
 */
const path = require('path');
const dirname = process.env.APP_WORK_DIR;
const NODE_ENV = process.env.NODE_ENV;
const fs = require('fs');
// https://pm2.keymetrics.io/docs/usage/application-declaration/
const envConfPath = path.resolve(dirname, './conf/port.conf');

let envConfContent = fs.readFileSync(envConfPath, 'utf-8');


let envConf = envConfContent.split('\n').reduce((res, conf) => {
    let confAfterReg = conf.match(/(\S*)\s*:\s*(\S*)/);
    if (!confAfterReg) {
        return res;
    }

    let [, key, val] = confAfterReg;
    res[key] = val;
    return res;
}, {});

console.log(envConf.LISTEN_PORT, '端口');

const config = {
    apps: [{
        name: 'fuwu_app',
        script: path.resolve(dirname, './index.js'),
        cwd: dirname,
        // 启动模式
        exec_mode: 'cluster',
        // 线层数，可根据线上机器核数来加实例数，每个实例由pm2进行负载均衡
        instances: "1",
        // 启动时间小于30s,则被认为异常退出，触发重新启动
        min_uptime: '30s',
        // 异常退出重启数
        max_restarts: 10,
        // https://pm2.keymetrics.io/docs/usage/restart-strategies/
        exp_backoff_restart_delay: 5000,
        // 最大内存限制数，超出自动重启
        max_memory_restart: '1500M',
        watch: false,
        ignore_watch: [],
        log_date_format: 'YYYY-MM-DD HH:mm:ss',
        error_file: path.resolve(dirname, './logs/pm2/error.log'),
        out_file: path.resolve(dirname, './logs/pm2/out.log'),
        merge_logs: true,
        env: {
            PORT: envConf.LISTEN_PORT,
            NODE_ENV: NODE_ENV
        }
        // 线下联调环境
        // watch_delay: 2000,
        // watch: ['app', 'conf', 'app.js'],
        // watch_options: {
        //     followSymlinks: false
        // }
    }]
};
module.exports = config;